package com.yuxi.util;

import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.yuxi.config.OkHttpClientConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * okhttp请求包装类
 * Created by yuxi on 16/10/27.
 */
public class OkHttpUtil {
   static final Logger logger = LoggerFactory.getLogger(OkHttpUtil.class);

    /**
     * okhttp请求
     * @param url
     * @return
     */
    public static Object getResultInfo(String url) {
        Response response = null;
        try {
            Request request = new Request.Builder().url(url).build();
            response = new OkHttpClientConfig().okHttpClient().newCall(request).execute();
            String result = response.body().string();
            logger.debug("testokhttp成功，url:'{}'，result:'{}'", url, result);
            return result;
        } catch (IOException e) {
            logger.error("testokhttp失败，url:'{}'", url);
        } finally {
            if (response.body() != null) {
                try {
                    response.body().close();//一定要关闭，不然会泄露资源
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";

    }
}
